package com.example.teacher.Controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.domain.Result;
import com.example.common.domain.dto.WorkDto;
import com.example.common.domain.pojo.*;
import com.example.common.domain.pojo.TypeHandler.JsonToObject;
import com.example.common.domain.vo.QuestionVo;
import com.example.teacher.Service.*;
import com.example.teacher.domain.dto.QuestionDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Api(value = "老师端接口", description = "获取ai生成数据")
@RestController
@RefreshScope//时时刷新配置信息
@Slf4j  //日志  log.info("登录日志测试：");
@RequestMapping("/teachers")
@RequiredArgsConstructor
public class AiAfterController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private HomeworkService homeworkService;
    @Autowired
    private PPTService pptService;
    @Autowired
    private HandoutsService handoutsService;
    @Autowired
    private ExperimentService experimentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private JsonToObject json;
    @Autowired
    private ClassStudentService classStudentService;
    @Autowired
    private ClassTeacherService classTeacherService;
    @Autowired
    private ClassesService classService;
    @Autowired
    private KnowledgePointService knowledgePointService;


    /**
     * 老师获取作业（条件分页查询）
     *
     * @param workDto
     */
    @PostMapping("/pageHomework")
    public Result<Page<Homework>> getHomeworkPage(@RequestBody WorkDto workDto) {
        Page<Homework> homeworkPage = homeworkService.queryHomeworkPage(workDto, workDto.getPageNum(), workDto.getPageSize());
        return Result.success(homeworkPage);
    }

    /**
     * 获取题目（老师条件获取自己课程的题目）
     *
     * @param questionDto
     */
    @PostMapping("/pageQuestions")
    public Result<Page<QuestionVo>> getQuestionPage(
            @RequestBody QuestionDto questionDto
    ) {
        Page<QuestionVo> questionPage = questionService.queryQuestionPage(questionDto, questionDto.getPageNum(), questionDto.getPageSize());
        return Result.success(questionPage);
    }

    /**
     * 通过homeworkId查看题目（老师，学生）
     */
    @GetMapping("/getQuestionsByHomeworkId/{homeworkId}")
    public Result<Page<QuestionVo>> getQuestionsByHomeworkId(@PathVariable String homeworkId,
                                                             @RequestParam(defaultValue = "1") Integer pageNum,
                                                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<QuestionVo> questionPage = questionService.selectQuestionPage(homeworkId, pageNum, pageSize);
        return Result.success(questionPage);
    }

    /**
     * 通过id获取题目
     */
    @GetMapping("/getQuestionById/{id}")
    public Result<QuestionVo> getQuestionById(@PathVariable String id) {
        QuestionVo questionVo = questionService.getQuestionById(id);
        return Result.success(questionVo);
    }


    /**
     * 通过id获取ppt（点击ppt数据获取ppt）
     *
     * @param id
     * @return
     */
    @GetMapping("/getPPTById/{id}")
    public Result<Ppt> getPPTById(@PathVariable String id) {
        Ppt ppt = pptService.getPPTById(id);
        return Result.success(ppt);
    }

    /**
     * 通过id获取讲义（点击讲义数据获取讲义）
     *
     * @param id
     * @return
     */
    @GetMapping("/getHandoutsById/{id}")
    public Result<Handouts> getHandoutsById(@PathVariable String id) {
        Handouts handouts = handoutsService.getHandoutsById(id);
        return Result.success(handouts);
    }

    /**
     * 通过id获取实验
     *
     * @param id
     * @return
     */
    @GetMapping("/getExperimentById/{id}")
    public Result<Experiment> getExperimentById(@PathVariable String id) {
        Experiment experiment = experimentService.getExperimentById(id);
        return Result.success(experiment);
    }

    /**
     * 通过id获取课程信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getCourseById/{id}")
    public Result<Course> getCourseById(@PathVariable String id) {
        Course course = courseService.getCourseByIdWithLogicalExpire(id);
        return Result.success(course);
    }

    /**
     * 通过老师id获取班级信息
     *
     * @param teacherId
     * @return
     */
    @GetMapping("/getClassById/{teacherId}")
    public Result<List<Classes>> getClassById(@PathVariable String teacherId) {
        List<ClassTeacher> classTeachers = classTeacherService.getClassById(teacherId);
        // 提取所有classId字段并转换为List<String>
        List<String> ids = classTeachers.stream()
                .map(ClassTeacher::getClassId) // 提取classId字段
                .filter(Objects::nonNull)      // 过滤可能的null值
                .map(Object::toString)         // 确保转换为String类型
                .collect(Collectors.toList()); // 收集结果到列表
        List<Classes> aClass = classService.getClassesByIds(ids);
        return Result.success(aClass);
    }

    /**
     * 通过班级id获取学生信息
     */
    @GetMapping("/getClassStudentById/{id}")
    public Result<List<ClassStudent>> getClassStudentById(@PathVariable String id) {
        List<ClassStudent> students = classStudentService.getClassStudentByIdWithLogicalExpire(id);
        return Result.success(students);
    }


    @ApiOperation("获取所教课程信息")
    @GetMapping("/getCourseByTeacherId/{teacherId}")
    public Result<Course> getCourseByTeacherId(@PathVariable String teacherId) {
        Course courses = courseService.getCourseByTeacherId(teacherId);
        return Result.success(courses);
    }

    @ApiOperation("通过课程获取课程知识点")
    @GetMapping("/getKnowledgeByCourseId/{courseId}")
    public Result<List<KnowledgePoint>> getKnowledgeByCourseId(@PathVariable String courseId) {
        List<KnowledgePoint> KnowledgePoints = knowledgePointService.getKnowledgeByCourseId(courseId);
        return Result.success(KnowledgePoints);
    }

    @ApiOperation("通过课程id分页获取知识点")
    @GetMapping("/getKnowledgeByCourseIdPage/{courseId}")
    public Result<Page<KnowledgePoint>> getKnowledgeByCourseIdPage(@PathVariable String courseId,
                                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<KnowledgePoint> KnowledgePoints = knowledgePointService.getKnowledgeByCourseIdPage(courseId, pageNum, pageSize);
        return Result.success(KnowledgePoints);
    }


}
